<?php

namespace App\Admin\Controllers\Commodity;

use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Model\Commodity_classify;

class ApiClassifyController extends Controller
{

    public function add_classify(Request $request)
    {
        $data = $request->input();
        if (empty($data))
            return return_array(1, '非法参数');


        $classify = new Commodity_classify;
        $classify_data = array(
            'classify_name' => $data['name'],
            'classify_index_img' => empty($data['img']) ? '' : $data['img'],
            'parent_id' => $data['parent']
        );

        if ($classify->add_commodity_classify($classify_data)) {
            if (empty($data["parent"]))
                return return_array(0, '添加父类成功', $classify->get_classify());
            else
                return return_array(0, '添加子类成功', $classify->get_classify($data["parent"]));
        } else
            return return_array(1, '未知错误');
    }

    public function update_classify(Request $request)
    {
        $data = $request->input();
        if (empty($data))
            return return_array(1, '非法参数');

        $classify = new Commodity_classify;
        if (empty($res = $classify->where('commodity_classify_id', '=', $data['id'])->first()))
            return return_array(1, '该分类不存在');
        else {
            $classify_data = array(
                'classify_name' => $data['name'],
                'classify_index_img' => empty($data['img']) ? '' : $data['img'],
                'parent_id' => $data['parent']
            );
            if (!$res->update_commodity_classify($classify_data))
                return return_array(1, "未知错误");
            else {
                if (empty($data["parent"]))
                    return return_array(0, '更新父类成功', $classify->get_classify());
                else
                    return return_array(0, '更新子类成功', $classify->get_classify($data["parent"]));
            }
        }
    }
    public function get_all_classify()
    {
        $classify = new Commodity_classify;
        return return_array(0, '获取成功', $classify->get_all_classify());
    }
    public function get_children_classify(Request $request)
    {
        $data = $request->input();
        if (empty($data))
            return return_array(1, '非法参数');
        $classify = new Commodity_classify;
        return return_array(0, '获取成功', $classify->get_classify($data["parent"]));
    }

    public function delele_classify(Request $request)
    {
        $data = $request->input();
        if (empty($data))
            return return_array(1, '非法参数');

        $classify = new Commodity_classify;

        if (empty($data["parent"])) {

            DB::beginTransaction();
            if (!$classify->delete_classify($data['id'])) {
                DB::rollBack();
                return return_array(1, '未知错误1');
            }
            try {
                $classify->delete_chidren_classify($data['id']);
            } catch (Exception $e) {
                DB::rollBack();
                return return_array(1, '未知错误2');
            }
//            if (!$classify->delete_chidren_classify($data['id'])) {
//                DB::rollBack();
//                return return_array(1, '未知错误2');
//            }
            DB::commit();
            return return_array(0, '删除成功', $classify->get_classify());
        } else {
            if (!$classify->delete_classify($data['id']))
                return return_array(1, '未知错误');
            else
                return return_array(0, '删除成功', $classify->get_classify($data["parent"]));

        }
    }
}
